<template>
  <div class="container">
    <!-- 头部区域 -->
    <van-nav-bar title="我的收货地址" left-text="返回" left-arrow @click-left="onleft" />
    <!-- 头部区域 -->

    <!-- 内容区域 -->
    <van-address-list v-model="chosenAddressId" :list="list" default-tag-text="默认" @add="$router.push('/addAddress')" @edit="onEdit" @select="onclick" />
    <!-- 内容区域 -->
  </div>
</template>

<script>
import { getAddress } from '@/api/address'
export default {
  name: 'shippingAddress',
  props: {
    siteshow: {
      type: Boolean
    }
  },
  components: {},
  data() {
    return {
      chosenAddressId: '',
      list: [],
      addressList: [],
      address: []
    }
  },
  methods: {
    // 获取收货地址列表
    async getAddressList() {
      const { data } = await getAddress()
      console.log(data)
      this.addressList = data.data
      var newArr = []
      data.data.forEach(val => {
        var lists = {}
        lists.id = val.receive_address_id
        lists.name = val.receive_user_name
        lists.tel = val.receive_user_phone
        this.address.push(val.receive_user_address)
        lists.address = val.receive_user_address.replace(/\+/g, '')
        if (val.is_default === 'yes') {
          lists.isDefault = true
          this.chosenAddressId = val.receive_address_id
        } else {
          lists.isDefault = false
        }
        newArr.push(lists)
      })

      this.list = newArr
    },
    // 修改地址入口
    onEdit(item, index) {
      this.$router.push('/editAddress')
      this.addressList[index].address = this.address[index]
      localStorage.setItem('addressMsg', JSON.stringify(this.addressList[index]))
    },
    onClickLeft() {},
    // 选中地址
    onclick(item) {
      // this.$store.commit('selectLocation', item)
      this.$emit('site-successfly', item)
    },
    onleft() {
      if (!this.siteshow) {
        this.$router.back()
      }
    }
  },
  created() {
    this.getAddressList()
  },
  computed: {},
  mounted() {},
  watch: {}
}
</script>
<style scoped lang="less"></style>
